<?php
/* 净水器类 */
namespace Api\Controller;
use Think\Controller;
use Think\Log;
class WaterPurifierController extends Controller {

	protected $userId = 0;  //用户ID
 	protected $model;
 	protected $registerTime;  //用户注册时间
	public function __construct()
	{
		//解决跨域
		echoHeader();
	    $this->model = D('WaterPurifier');
	    $this->userId = I('post.user_id', 0, 'intval');  
		if(ACTION_NAME != 'introduce' && ACTION_NAME != 'checkOrder'){
	        if ($this->userId <= 0) {
	        	$this->ajaxReturn(array('err_code'=>20001, 'err_msg'=>'用户不能为空'), 'JSON');
	        }
	        $map['user_id'] = $this->userId;
	        $user=M('user')->where($map)->find();
	        if (empty($user)) {
	        	$this->ajaxReturn(array('err_code'=>20002,'err_msg'=>'用户不存在'),'JSON');
	        }elseif($user['identity'] != 1){ //净水器功能只有业主可用
	        	$this->ajaxReturn(array('err_code'=>20003,'err_msg'=>'用户登录角色错误'),'JSON');
	        }
	        $this->registerTime = $user['add_time'];
		}
	}

	//净水器介绍
	public function introduce()
	{
		$brand = I('post.brand', 1, 'intval');
		$introduce = $this->model->getIntroduce($brand);
		$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$introduce));
	}

	//是否领取
	public function isReceive()
	{
		$brand = I('post.brand', 1, 'intval');
		$isReceive = $this->model->checkReceive($this->userId);
		if ($isReceive) {
			$this->ajaxReturn(array('err_code'=>200, 'err_msg'=>$isReceive));
		}else{
			$this->ajaxReturn(array('err_code'=>200, 'err_msg'=>"0"));
		}
	}

	public function index()
	{
		$data = array();
		$brand = I('post.brand', 1, 'intval');
		$data['introduce'] = $this->model->getIntroduce($brand);
		$isReceive = $this->model->checkReceive($this->userId);
		if ($isReceive) {
			$data['replace'] = $this->model->getReplaceInfo($this->userId, $brand);
			$data['repair']  = $this->model->getRepairInfo($brand);
		}else{
			$data['receive'] = $this->model->getReceiveInfo($this->registerTime, $brand);
		}
		$address = $this->model->getLastAddress($this->userId, $isReceive);
		if($address){
			$data['address'] = $address;
		}else{
			$data['address'] = null;
		}
		$data['is_receive'] = $isReceive ? true : false;
		$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$data));
	}

	//下单
	public function order()
	{
		$res = $this->model->doOrder($this->userId, $this->registerTime);

		if ($res) {
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>'下单成功'));
		}else{
			$this->ajaxReturn($this->model->getError());
		}
	}

	//服务记录
  	public function serviceRecord()
  	{
  		$res = $this->model->getServiceRecord($this->userId);
  		if ($res) {
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$res));
		}else{
			$this->ajaxReturn(array('err_code'=>20001,'err_msg'=>'查找不到对应信息'));
		}
  	}

  	//订单详情
  	public function orderDetail()
  	{
  		$orderId = I('post.order_id', 0, 'trim');

  		$order = $this->model->getOrderInfo($this->userId, $orderId);
  		if ($order) {
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$order));
		}else{
			$this->ajaxReturn($this->model->getError());
		}
  	}

  	//完成安装
  	public function finish()
  	{
  		$orderId = I('post.order_id', 0, 'intval');
  		$res = $this->model->finish($this->userId, $orderId);
  		if ($res) {
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$res));
		}else{
			$this->ajaxReturn($this->model->getError());
		}
  	}

  	//个人中心订单列表
  	public function orderList()
  	{
  		$orderType = I('post.order_type', 0, 'intval');
  		$res = $this->model->getOrderList($this->userId, $orderType);
  		if ($res) {
			$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$res));
		}else{
			$this->ajaxReturn($this->model->getError());
		}
  	}

  	//检查用户订单状态
  	public function checkOrder()
  	{
  		$res = $this->model->checkOrder($this->userId);
  		if($res['type'] == 'order'){
  			if($res['data']['finish_time'] > 0){
  				$msg = $res['data']['brand'];
  				$type = 'brand';
  			}else{
  				$msg = $res['data']['order_id'];
  				$type = 'order';
  			}
  		}else{
  			$msg = $res['data'];
  			$type = 'icon';
  		}
  		$this->ajaxReturn(array('err_code'=>200, 'err_msg'=>$msg, 'type'=>$type));
  	// 	if ($res) {
  	// 		$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$res));
  	// 	}else{
			// $this->ajaxReturn(array('err_code'=>200,'err_msg'=>0));
  	// 	}
  	}
  	
  	//订单状态
  // 	public function orderStatus()
  // 	{
  // 		$orderId = I('post.order_id', 0, 'intval');
  // 		$res = $this->model->orderStatus($this->userId, $orderId);
  // 		if ($res) {
		// 	$this->ajaxReturn(array('err_code'=>200,'err_msg'=>$res));
		// }else{
		// 	$this->ajaxReturn($this->model->getError());
		// }	
  // 	}
}